package com.ruoyi.web.service.Impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.mes.*;
import com.ruoyi.web.domain.vo.MaterialInVo;
import com.ruoyi.web.mapper.MesMaterialMapper;
import com.ruoyi.web.mapper.MesConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.mapper.MesMaterialInMapper;
import com.ruoyi.web.service.IMesMaterialInService;

import javax.annotation.Resource;

/**
 * 物料入库Service业务层处理
 *
 * @author levicash
 * @date 2024-01-26
 */
@Service
public class MesMaterialInServiceImpl implements IMesMaterialInService {
    @Resource
    private MesMaterialInMapper mesMaterialInMapper;

    @Resource
    private MesMaterialMapper mesMaterialMapper;

    @Resource
    private MesConfigMapper configMapper;

    /**
     * 查询物料入库
     *
     * @param id 物料入库主键
     * @return 物料入库
     */
    @Override
    public MesMaterialIn selectMesMaterialInById(Long id) {
        return mesMaterialInMapper.selectMesMaterialInById(id);
    }

    /**
     * 查询物料入库列表
     *
     * @param mesMaterialIn 物料入库
     * @return 物料入库
     */
    @Override
    public List<MesMaterialIn> selectMesMaterialInList(MesMaterialIn mesMaterialIn) {
        return mesMaterialInMapper.selectMesMaterialInList(mesMaterialIn);
    }

    /**
     * 新增物料入库
     *
     * @param mesMaterialIn 物料入库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesMaterialIn(MesMaterialIn mesMaterialIn) {
        mesMaterialIn.setCreateTime(DateUtils.getNowDate());
        mesMaterialIn.setCreateBy(SecurityUtils.getUsername());
        // 生成入库单编号
        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "code");
        map.put("tableName", "mes_material_in");
        int maxNum = configMapper.getMaxId(map);
        String code = PinYinUtils.getCodeNormal(maxNum, "WLRKD");
        mesMaterialIn.setCode(code);
        int rows = mesMaterialInMapper.insertMesMaterialIn(mesMaterialIn);
        insertMesMaterialIndetail(mesMaterialIn);
        return rows;
    }

    /**
     * 修改物料入库
     *
     * @param mesMaterialIn 物料入库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesMaterialIn(MesMaterialIn mesMaterialIn) {
        mesMaterialIn.setUpdateTime(DateUtils.getNowDate());
        mesMaterialIn.setUpdateBy(SecurityUtils.getUsername());
        int status = mesMaterialIn.getStatus();
        if (status == 1) {// 提交
            // 修改物料入库明细的仓库数量(减库存)
            MesMaterialIn Info = mesMaterialInMapper.selectMesMaterialInById(mesMaterialIn.getId()); // 查询数据库
            List<MesMaterialIndetail> mesMaterialIndetailList = Info.getMesMaterialIndetailList();
            if (mesMaterialIndetailList.size() > 0) {
                // 获取关联ids
                List<Long> listId = mesMaterialIndetailList.stream().map(it -> it.getMaterialId()).collect(Collectors.toList());
                Long[] ids = listId.toArray(new Long[0]);
                // 根据关联ids查询物料明细
                List<MesMaterial> detailList = mesMaterialMapper.selectMesMaterialByIds(ids);
                // 遍历明细修改库存
                for (MesMaterialIndetail mesMaterialIndetail : mesMaterialIndetailList) {
                    for (MesMaterial detailItem : detailList) {
                        if (detailItem.getId().equals(mesMaterialIndetail.getMaterialId())) {
                            BigDecimal stockNum = (detailItem.getQuantity() == null ? BigDecimal.ZERO : detailItem.getQuantity()); // 库存数量
                            BigDecimal quantity = mesMaterialIndetail.getQuantity(); // 入库数量
                            // int result = stockNum.compareTo(quantity);

                            BigDecimal difference = stockNum.add(quantity);
                            detailItem.setQuantity(difference);         // 更改库存数量
                            detailItem.setStockPrice(difference.multiply(detailItem.getRealtimePrice()));  // 更改金额
                        }
                    }
                }
                // 批量修改物料明细库存
                mesMaterialMapper.updateStockNumByBatch(detailList);
            }
        } else {
            mesMaterialInMapper.deleteMesMaterialIndetailByMaterialinId(mesMaterialIn.getId());
            insertMesMaterialIndetail(mesMaterialIn);
        }

        return mesMaterialInMapper.updateMesMaterialIn(mesMaterialIn);
    }

    /**
     * 批量删除物料入库
     *
     * @param ids 需要删除的物料入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMaterialInByIds(Long[] ids) {
        mesMaterialInMapper.deleteMesMaterialIndetailByMaterialinIds(ids);
        return mesMaterialInMapper.deleteMesMaterialInByIds(ids);
    }

    /**
     * 删除物料入库信息
     *
     * @param id 物料入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMaterialInById(Long id) {
        mesMaterialInMapper.deleteMesMaterialIndetailByMaterialinId(id);
        return mesMaterialInMapper.deleteMesMaterialInById(id);
    }

    /**
     * 新增物料入库明细表信息
     *
     * @param mesMaterialIn 物料入库对象
     */
    public void insertMesMaterialIndetail(MesMaterialIn mesMaterialIn) {
        List<MesMaterialIndetail> mesMaterialIndetailList = mesMaterialIn.getMesMaterialIndetailList();
        Long id = mesMaterialIn.getId();
        if (StringUtils.isNotNull(mesMaterialIndetailList)) {
            List<MesMaterialIndetail> list = new ArrayList<MesMaterialIndetail>();
            for (MesMaterialIndetail mesMaterialIndetail : mesMaterialIndetailList) {
                mesMaterialIndetail.setCreateBy(SecurityUtils.getUsername());
                mesMaterialIndetail.setCreateTime(DateUtils.getNowDate());
                mesMaterialIndetail.setMaterialinId(id);
                list.add(mesMaterialIndetail);
            }
            if (list.size() > 0) {
                mesMaterialInMapper.batchMesMaterialIndetail(list);
            }
        }
    }


    /**
     * 查询物料入库明细
     *
     * @param map 查询参数
     * @return 物料入库关联集合
     */
    @Override
    public List<MaterialInVo> selectMesMaterialInDetailList(Map<String, Object> map) {
        return mesMaterialInMapper.selectMesMaterialInDetailList(map);
    }
}
